package spring.cloud.tasks.common.nodepaths;

import spring.cloud.tasks.common.Symbols;
import spring.cloud.tasks.common.curator.Curator;

import java.util.ArrayList;
import java.util.List;


public class TaskItemNodePaths {
    public static final String ROOT = Symbols.SLASH + "task_items";

    public static String getTaskInstancesNodePath(String taskId) {
        return ROOT + Symbols.SLASH + taskId;
    }

    public static String getTaskStagesNodePath(String taskId, String taskInstanceId) {
        return getTaskInstancesNodePath(taskId) + Symbols.SLASH + taskInstanceId;
    }

    public static final String getTaskStage$typesNodePath(String taskId, String taskInstanceId, String taskStage) {
        return getTaskStagesNodePath(taskId, taskInstanceId) + Symbols.SLASH + taskStage;
    }

    public static final String getTaskStage$type$indexsNodePath(String taskId, String taskInstanceId, String taskStage, String taskStage$type) {
        return getTaskStage$typesNodePath(taskId, taskInstanceId, taskStage) + Symbols.SLASH + taskStage$type;
    }

    public static final String getTaskStage$type$indexNodePath(String taskId, String taskInstanceId, String taskStage, String taskStage$type, String taskStage$type$index) {
        return getTaskStage$type$indexsNodePath(taskId, taskInstanceId, taskStage, taskStage$type) + Symbols.SLASH + taskStage$type$index;
    }

    //
    public static List<String> getTaskItemPathList(Curator curator, String taskId) throws Exception {
        List<String> oldTaskItemIndexPathList = new ArrayList<String>();
        List<String> taskInstanceIdList = curator.getChildren(TaskItemNodePaths.getTaskInstancesNodePath(taskId));
        if (taskInstanceIdList != null) {
            for (String taskInstanceId : taskInstanceIdList) {
                List<String> taskStageList = curator.getChildren(TaskItemNodePaths.getTaskStagesNodePath(taskId, taskInstanceId));
                if (taskStageList != null) {
                    for (String taskStage : taskStageList) {
                        List<String> taskStage$typeList = curator.getChildren(TaskItemNodePaths.getTaskStage$typesNodePath(taskId, taskInstanceId, taskStage));
                        if (taskStage$typeList != null) {
                            for (String taskStage$type : taskStage$typeList) {
                                List<String> taskStage$type$indexList = curator.getChildren(TaskItemNodePaths.getTaskStage$type$indexsNodePath(taskId, taskInstanceId, taskStage, taskStage$type));
                                if (taskStage$type$indexList != null) {
                                    for (String taskStage$type$index : taskStage$type$indexList) {
                                        String taskItemIndexPath = TaskItemNodePaths.getTaskStage$type$indexNodePath(taskId, taskInstanceId, taskStage, taskStage$type, taskStage$type$index);
                                        oldTaskItemIndexPathList.add(taskItemIndexPath);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return oldTaskItemIndexPathList;
    }

}
